/* Copyright 2012-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.snakerflow.framework.flow;

import java.util.ArrayList;
import java.util.List;

import org.snaker.engine.impl.GeneralAccessStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.snakerflow.framework.flow.dao.ApprovalDao;

/**
 * 自定义访问策略，根据操作人获取其所有组集合（部门、角色、权限）
 * @author 何东
 * @since 0.1
 */
public class CustomAccessStrategy extends GeneralAccessStrategy {
	
	/** 操作员 状态（正常） */
	private static final String OPERATOR_STATUS = "running";
	
	@Autowired
	ApprovalDao approvalDao;
	
	@SuppressWarnings("rawtypes")
	protected List<String> ensureGroup(String operator) {
//		return ShiroUtils.getGroups();
		
		List<String> groups = new ArrayList<String>();
		
		StringBuilder sql = new StringBuilder();
		sql.append("select t1.rolename ");
		sql.append("  from AC_ROLE t1, AC_OPERATORROLE t2, AC_OPERATOR t3");
		sql.append(" where t2.roleid = t1.roleid");
		sql.append("   and t3.operatorid = t2.operatorid");
		sql.append("   and t3.status = ?");
		sql.append("   and t3.userid = ?");
		
		List roleNames = approvalDao.createSQLQuery(sql.toString(), OPERATOR_STATUS, operator).list();
		if (!CollectionUtils.isEmpty(roleNames)) {
			for (Object obj : roleNames) {
				groups.add(obj.toString());
			}
		}
		
		return groups;
	}
}
